from flask import Flask
from flask import render_template
from flask import request
from flask_sqlalchemy import SQLAlchemy
import pymysql
pymysql.install_as_MySQLdb()


app = Flask(__name__)

app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:myw0501165@localhost:3306/wangzhe'
app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True
db = SQLAlchemy(app)


class Hero(db.Model):
    __tablename__ = 'hero'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(30))
    avautar = db.Column(db.String(255))
    picture = db.Column(db.String(255))
    hero_story = db.Column(db.Text)
    skills = db.relationship("Skill", backref="hero", lazy="dynamic")


class Skill(db.Model):
    __tablename__ = 'skill'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    skill_name = db.Column(db.String(30))
    skill_content = db.Column(db.String(255))
    skill_exp = db.Column(db.String(255))
    skill_pic = db.Column(db.String(255))
    skill_hero_id = db.Column(db.Integer, db.ForeignKey('hero.id'))

db.create_all()

@app.route('/', methods=['GET', 'POST'])
def index():
    if request.method == 'GET':
        heros = Hero.query.all()
        skills = Skill.query.all()
        return render_template('index.html', params=locals())
    

@app.route('/hero_detail')
def hero_detail():
    return render_template('detail.html')


if __name__ == '__main__':
    app.run(debug=True)
